package com.lxyk.sgq;

import com.lxyk.sgq.pojo.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@RestController
@RefreshScope
public class NacosConfigApplication {

    @Value("${person.name}")
    private String name;

    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @Autowired
    private MyDataSource myDataSource;

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class,args);
    }

    @GetMapping("/getConfig1")
    public String getConfig1(){
        return name;
    }


 @GetMapping("/getConfig2")
    public String getConfig2(){
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String age = environment.getProperty("person.age");
        return age;
    }


    @GetMapping("/getConfig3")
    public Map getConfig3(){
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String password = environment.getProperty("spring.datasource.password");
        String name = environment.getProperty("name");
        String school = environment.getProperty("school");
        String update = environment.getProperty("update");
        String personName = environment.getProperty("person.name");
        HashMap<String, Object> map = new HashMap(){{
              put("demo1.yml->password",password);
              put("demo2.yml->name",name);
              put("demo2.yml->school",school);
              put("demo3.yml->update",update);
              put("demo4.properties->personName",personName);
        }};
        return map;
    }

    @GetMapping("/getExtensionConfig")
    public MyDataSource getExtensionConfig(){
       return myDataSource;
    }
}
